]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
jsParse: Unnest functions
authorFlorian Müllner <fmuellner@gnome.org>
Tue, 20 Aug 2019 00:24:28 +0000 (02:24 +0200)
committerGeorges Basile Stavracas Neto <georges.stavracas@gmail.com>
Mon, 11 Nov 2019 19:25:14 +0000 (19:25 +0000)
Nesting functions can be helpful for private helper functions, but
here they are accessing some variables from the outer scope and
shadowing others. Split them out to avoid any ambiguity.

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/805

js/misc/jsParse.js

index 8cfff1659a81a06b26c99fa4a8b3654e4356b95d..de8f66f82ba06ec011869f8a7a7c6f6b9e468fe7 100644 (file)
@@ -84,29 +84,28 @@ function findMatchingBrace(expr, offset) {
     let closeBrace = expr.charAt(offset);
     let openBrace = ({ ')': '(', ']': '[' })[closeBrace];
 
-    function findTheBrace(expr, offset) {
-        if (offset < 0) {
-            return -1;
-        }
+    return findTheBrace(expr, offset - 1, openBrace, closeBrace);
+}
 
-        if (expr.charAt(offset) == openBrace) {
-            return offset;
-        }
-        if (expr.charAt(offset).match(/['"]/)) {
-            return findTheBrace(expr, findMatchingQuote(expr, offset) - 1);
-        }
-        if (expr.charAt(offset) == '/') {
-            return findTheBrace(expr, findMatchingSlash(expr, offset) - 1);
-        }
-        if (expr.charAt(offset) == closeBrace) {
-            return findTheBrace(expr, findTheBrace(expr, offset - 1) - 1);
-        }
+function findTheBrace(expr, offset, ...braces) {
+    let [openBrace, closeBrace] = braces;
 
-        return findTheBrace(expr, offset - 1);
+    if (offset < 0)
+        return -1;
 
-    }
+    if (expr.charAt(offset) == openBrace)
+        return offset;
+
+    if (expr.charAt(offset).match(/['"]/))
+        return findTheBrace(expr, findMatchingQuote(expr, offset) - 1, ...braces);
+
+    if (expr.charAt(offset) == '/')
+        return findTheBrace(expr, findMatchingSlash(expr, offset) - 1, ...braces);
+
+    if (expr.charAt(offset) == closeBrace)
+        return findTheBrace(expr, findTheBrace(expr, offset - 1, ...braces) - 1, ...braces);
 
-    return findTheBrace(expr, offset - 1);
+    return findTheBrace(expr, offset - 1, ...braces);
 }
 
 // Walk expr backwards from offset looking for the beginning of an
@@ -189,24 +188,26 @@ function getCommonPrefix(words) {
     return word;
 }
 
+// Remove any blocks that are quoted or are in a regex
+function removeLiterals(str) {
+    if (str.length == 0)
+        return '';
+
+    let currChar = str.charAt(str.length - 1);
+    if (currChar == '"' || currChar == '\'') {
+        return removeLiterals(
+            str.slice(0, findMatchingQuote(str, str.length - 1)));
+    } else if (currChar == '/') {
+        return removeLiterals(
+            str.slice(0, findMatchingSlash(str, str.length - 1)));
+    }
+
+    return removeLiterals(str.slice(0, str.length - 1)) + currChar;
+}
+
 // Returns true if there is reason to think that eval(str)
 // will modify the global scope
 function isUnsafeExpression(str) {
-    // Remove any blocks that are quoted or are in a regex
-    function removeLiterals(str) {
-        if (str.length == 0) {
-            return '';
-        }
-
-        let currChar = str.charAt(str.length - 1);
-        if (currChar == '"' || currChar == '\'') {
-            return removeLiterals(str.slice(0, findMatchingQuote(str, str.length - 1)));
-        } else if (currChar == '/') {
-            return removeLiterals(str.slice(0, findMatchingSlash(str, str.length - 1)));
-        }
-
-        return removeLiterals(str.slice(0, str.length - 1)) + currChar;
-    }
 
     // Check for any sort of assignment
     // The strategy used is dumb: remove any quotes